<?php
/**
 * Created by PhpStorm.
 * User: dwlw
 * Date: 2018/5/10
 * Time: 10:04
 */
namespace app\Admin\controller;
use app\facade\Power;
class Auth extends Base{
    //权限模板列表
    public function lst(){
        $auth = new \app\admin\model\Auth();
        $auth_lst = $auth->get_auth_model('auth_model = 1');
        $this->assign('auth_lst',$auth_lst);
        return $this->fetch();
    }

    /**设置权限模板/设置权限
     * @return mixed
     */
    public function add(){
        if ($this->request->isPost()){
            $data = $this->request->param();
            $id = $this->request->param('id');
            if (Power::set_auth_model($data,$id)){
                $this->success('添加成功','auth/lst');
            }else{
                $this->error('添加失败');
            }
        }else{
            $auth = new \app\admin\model\Auth();
            //角色表
            $role = $auth->get_auth_role();
            $this->assign('role',$role);
            return $this->fetch();
        }
    }
    //权限模板修改
    public function edit(){
        if ($this->request->isPost()){
            $data = $this->request->param();
            $id = $this->request->param('id');
            if (Power::set_auth_model($data,$id)){
                $this->success('修改成功','auth/lst');
            }else{
                $this->error('修改失败,权限不足');
            }
        }else{
            $id = $this->request->param('id');
            $this->assign('id',$id);
            $auth = new \app\admin\model\Auth();
            //角色表
            $role = $auth->get_auth_role();
            $this->assign('role',$role);
            //获取角色模板
            $res = $auth->get_auth_model('id='.$id);
            $this->assign('auth',$res[0]);
            $role_arr = [];
            if (is_array($res[0]['auth_fun'])) {
                foreach ($res[0]['auth_fun'] as $v) {
                    $role_arr[] = $v['id'];
                }
            }
            $this->assign('role_arr',$role_arr);
            return $this->fetch();
        }
    }

    public function delete(){
        $id = $this->request->param('id');
        $auth = new Auth();
        $auth ->table('d_auth_fun_role')->where('id='.$id)->delete();
        if ($auth){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
        return $this->fetch();
    }

}